//
//  MPResponse.m
//  SearchPeople
//
//  Created by zk on 16/5/10.
//
//

#import "MPResponse.h"

@implementation MPResponse

#pragma mark - 初始化

- (id)initWithResponseDic:(NSDictionary *)response{
    self = [super init];
    if (self) {
        [self initializeWithDic:response];
    }
    return self;
}

- (void)initializeWithDic:(NSDictionary *)response{
    _isSuccess = NO;
    if (response == nil) {
        self.errorMsg = DATA_FORMAT_ERROR;
        return;
    }
    
    if ([[response objectForKey:@"code"] longValue] == 0) {
        _isSuccess = YES;
    } else {
        _isSuccess = NO;
    }
    
    //以2种形式的返回数据错误判断
    if (_isSuccess == NO && [response objectForKey:@"msg"]!= nil) {
        self.errorMsg = [NSString stringWithFormat:@"%@",[response objectForKey:@"msg"]];
    }
    
    if (_isSuccess == NO && [response objectForKey:@"msg"]) {
        self.errorMsg = DATA_FORMAT_ERROR;
    }
    
    NSDictionary *result = [response objectForKey:@"data"];
    
    if ([result isKindOfClass:[NSDictionary class]]) {
        _isSuccess = YES;
        self.result = result;
    } else if ([result isKindOfClass:[NSArray class]]) {
        _isSuccess = YES;
        self.result = [NSDictionary dictionaryWithObject:result forKey:@"data"];
    } else if (result != nil) {
        self.result = [NSDictionary dictionaryWithObject:result forKey:@"data"];
    } else {
        _isSuccess = NO;
        self.errorMsg = DATA_FORMAT_ERROR;
    }
}

- (void)loadResultData:(NSDictionary *)resultData{
    _isSuccess = NO;
    if (resultData == nil) {
        self.errorMsg = DATA_FORMAT_ERROR;
        return;
    }
    
    if ([[resultData objectForKey:@"code"] integerValue] == 0) {
        _isSuccess = YES;
    } else {
        _isSuccess = NO;
    }
    
    //二种形式的返回数据错误判断
    if (_isSuccess == NO && [resultData objectForKey:@"msg"]!= nil ) {
        self.errorMsg = [NSString stringWithFormat:@"%@",[resultData objectForKey:@"msg"]];
        return;
    }
    if (_isSuccess == NO && [resultData objectForKey:@"msg"]== nil ) {
        self.errorMsg = DATA_FORMAT_ERROR;
        return;
    }
    //后台 把返回的上传图片返回的数据放在msg里了_(:з」∠)_
    if (_isSuccess == YES && [resultData objectForKey:@"msg"]!= nil ) {
        self.errorMsg = [NSString stringWithFormat:@"%@",[resultData objectForKey:@"msg"]];
    }
    
    NSDictionary *result = [resultData objectForKey:@"data"];
    
    if ([result isKindOfClass:[NSDictionary class]]) {
        _isSuccess = YES;
        self.result = result;
    }else if ([result isKindOfClass:[NSArray class]]){
        _isSuccess = YES;
        self.result = [NSDictionary dictionaryWithObject:result forKey:@"data"];
    }else if(result != nil){
        self.result = [NSDictionary dictionaryWithObject:result forKey:@"data"];
    } else {
        _isSuccess = YES;
        self.errorMsg = [NSString stringWithFormat:@"%@",[resultData objectForKey:@"msg"]];
        self.result = nil;
    }
}

- (NSString *)description{
    NSMutableString *descripString = [NSMutableString stringWithFormat:@""];
    [descripString appendString:@"\n========================Response Info===========================\n"];
    [descripString appendFormat:@"Response Name:%@\n",self.responseName];
    [descripString appendFormat:@"Response message:%@\n",self.errorMsg];
    [descripString appendFormat:@"Response Content:\n%@\n",self.result];
    [descripString appendString:@"===============================================================\n"];
    return descripString;

}

@end
